<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/utils/CalendarUtils.php'); 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AbstractEntity
 *
 * @author virtual
 */
abstract class AbstractEntity {
    private $tagChildList = array();
    
    //if an attribute is added to this list, it will not be rendered as
    //an attribute but as a child element
    protected function addTagAsChild($attrName) {
        $this->tagChildList[$attrName] = $attrName;
    }
    
    
    public function toXml() {
       
        //find all get methods;
        $r = new ReflectionClass($this);
        
        $tagname = strtolower(substr($r->name, 0,1)) . substr($r->name, 1);
        $attributes = "";
        $body = "";
        
        foreach($r->getMethods() as $methodname) {
            if(substr($methodname->name, 0,3) == "get") {
                
                $val = $methodname->invoke($this);
                $type = gettype($val);
                $attrName = strtolower(substr($methodname->name, 3,1)) . substr($methodname->name, 4);
                
               switch($type){
                    case "boolean":
                    case "integer": 
                    case "string": 
                        if(!array_key_exists($attrName, $this->tagChildList)){
                            $attributes .= " $attrName=\"$val\""; 
                        }else {
                            $body = "<$attrName><![CDATA[".$val."]]></$attrName>";
                        }
                        
                        break; 
                    case "object": 
                        
                        if(get_class($val) == "DateTime" ){ //if object is DateTime
                          //  $attributes .= " $attrName=\"".CalendarUtils::transformDateToTimestamp($val)."\"";
                        }else {
                            $toXmlMethod = new ReflectionMethod($val, "toXml");
                            $body .= $toXmlMethod->invoke($this);
                        }
                         break;
                    case "array": 
                        if(count($val) > 0){
                            $body .= "<$attrName>"; 
                            foreach($val as $obj){
                                $toXmlMethod = new ReflectionMethod($obj, "toXml");
                                $body .= $toXmlMethod->invoke($obj);
                            }
                            $body .= "</$attrName>"; 
                }
                        
                        break;
                    case "NULL": break;
                    default: echo "Abstract Entity - unknow type: " . $type;
                }
            }
        }
        
        return "<$tagname$attributes>$body</$tagname>";
    }
    
    public function booleanToYesNo($bool){
        if($bool)
            return "yes";
        else
            return "no";
    }
}

?>
